<?php

App::uses('AppController', 'Controller');

/**
 * Drivers Controller
 *
 * @property Driver $Driver
 */
class DriversController extends AppController {

    function beforeFilter() {
//    $this->Auth->allow('index');
        $this->set('title_for_layout', 'Choferes');
    }

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Driver->recursive = 0;
        $this->paginate = array('order' => 'Driver.id desc', 'limit' => 50);
        $this->set('drivers', $this->paginate());
    }

    //  search by Transport(Company name)
    public function search() {
        $name = '%' . trim($this->request->data('name')) . '%';
        $this->paginate = array(
            'conditions' => array('Company.social_razon LIKE' => $name));
        $this->set('drivers', $this->paginate());
        $this->layout = 'ajax';
    }

    public function selectChange() {
        $options = array('conditions' => array('Driver.company_id' => $this->request->data('idCompany')));
        $this->set('drivers', $this->Driver->find('list', $options));
        $this->layout = 'ajax';
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Driver->exists($id)) {
            throw new NotFoundException(__('Invalid driver'));
        }
        $options = array('conditions' => array('Driver.' . $this->Driver->primaryKey => $id));
        $this->set('driver', $this->Driver->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Driver->create();
            if ($this->Driver->save($this->request->data)) {
                $this->Session->setFlash(__('Chofer Agregado exitosamente'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Chofer No Agregado'));
            }
        }
        $companies = $this->Driver->Company->find('list');
        $vehicles = $this->requestAction('/Vehicles/getVehiclesList');
        $coupleds = $this->requestAction('/Coupleds/getCoupledsList');
        $this->set(compact('companies', 'vehicles', 'coupleds'));
    }

    public function addAll() {

        if (!empty($this->request->data['Driver']['company_id'])):
            $company_id = $this->request->data['Driver']['company_id'];
        else:
            $company_id = 0;
        endif;

        if ($this->request->is('post')) {

            $idVehicle = $this->requestAction('/Vehicles/addAll/' . $this->request->data['vehicleModel'] . '/' . $this->request->data['vehicleDomain'] . '/' . $company_id);
            $idCoupled = $this->requestAction('/Coupleds/addAll/' . $this->request->data['driverModel'] . '/' . $this->request->data['driverDomain'] . '/' . $company_id);

            if (isset($idVehicle) && isset($idCoupled)) {

                $data = array(
                    'Driver' => array('company_id' => $company_id,
                        'vehicle_id' => $idVehicle, 'coupled_id' => $idCoupled,
                        'name' => $this->request->data['Driver']['name'],
                        'dni' => $this->request->data['Driver']['dni'],
                        'cuil' => $this->request->data['Driver']['cuil'],
                        'phone' => $this->request->data['Driver']['phone'],
                        'city' => $this->request->data['Driver']['city'],
                    )
                );


                $this->Driver->create();
                if ($this->Driver->save($data)) {
                    $this->Session->setFlash(__('Chofer Agregado exitosamente'));
                    $this->redirect(array('action' => 'index'));
                }
            } else {
                $this->Session->setFlash(__('Chofer No Agregado, intente nuevamente'));
            }
        }
        $data = $this->request->data;
        $companies = $this->Driver->Company->find('list');
        $vehicles = $this->requestAction('/Vehicles/getVehiclesList');
        $coupleds = $this->requestAction('/Coupleds/getCoupledsList');
        $this->set(compact('companies', 'vehicles', 'coupleds', 'data'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Driver->exists($id)) {
            throw new NotFoundException(__('Invalid driver'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Driver->save($this->request->data)) {
                $this->Session->setFlash(__('Chofer actualizado exitosamente'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Chofer No actualizado'));
            }
        } else {
            $options = array('conditions' => array('Driver.' . $this->Driver->primaryKey => $id));
            $this->request->data = $this->Driver->find('first', $options);
        }
        $companies = $this->Driver->Company->find('list');
        $vehicles = $this->requestAction('/Vehicles/getVehiclesList');
        $coupleds = $this->requestAction('/Coupleds/getCoupledsList');
        $this->set(compact('companies', 'vehicles', 'coupleds'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Driver->id = $id;
        if (!$this->Driver->exists()) {
            throw new NotFoundException(__('Invalid driver'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Driver->delete()) {
            $this->Session->setFlash(__('Chofer Borrado exitosamente'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Chofer No borrado'));
        $this->redirect(array('action' => 'index'));
    }

}
